<html>
<head><meta charset="utf-8"><title>rust-analyzer#720 macro_rules · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html">rust-analyzer#720 macro_rules</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="159803511"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/159803511" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#159803511">(Mar 02 2019 at 15:58)</a>:</h4>
<p><span class="user-mention" data-user-id="133169">@matklad</span> Did you have some specific error type in mind for error handling (<code>failure::Error</code> for example)? Or I could just start with something simple</p>



<a name="159803881"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/159803881" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#159803881">(Mar 02 2019 at 16:08)</a>:</h4>
<p>I think <code>failure::Error</code> will definitely be a bad fit there</p>



<a name="159803889"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/159803889" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#159803889">(Mar 02 2019 at 16:09)</a>:</h4>
<p>Starting with simple, but specific error type is a good aproach</p>



<a name="159803903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/159803903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#159803903">(Mar 02 2019 at 16:09)</a>:</h4>
<p>that is, as long as it is some opaque <code>struct MacroParsingError</code> we can always enchance it :)</p>



<a name="159803947"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/159803947" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#159803947">(Mar 02 2019 at 16:10)</a>:</h4>
<p>Ah, I can't just replace option with result everywhere, because things like <code>expand_rule</code> are allowed to not match, if I understand correctly</p>



<a name="159804165"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/159804165" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#159804165">(Mar 02 2019 at 16:17)</a>:</h4>
<p>yeah, thats the part of the problem: currently "there's an error" and "threre's a result, and it is empty" cases are conflated</p>



<a name="159804190"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/159804190" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#159804190">(Mar 02 2019 at 16:18)</a>:</h4>
<p>Right</p>



<a name="159804231"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/159804231" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#159804231">(Mar 02 2019 at 16:18)</a>:</h4>
<p>So error cases should propagate, and then panic or something to start off with, and empty cases are propagated like now</p>



<a name="159804714"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/159804714" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#159804714">(Mar 02 2019 at 16:31)</a>:</h4>
<p>yeah</p>



<a name="159804724"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/159804724" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#159804724">(Mar 02 2019 at 16:31)</a>:</h4>
<p>Though I think it makes sense to perhaps try a "best-effort" parse as well?</p>



<a name="159804774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/159804774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#159804774">(Mar 02 2019 at 16:32)</a>:</h4>
<p>Say, the task is parsing <code>macro_rules</code> token tree into a mbe defenition</p>



<a name="159804779"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/159804779" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#159804779">(Mar 02 2019 at 16:32)</a>:</h4>
<p>ideally, if there's an error in a single branch, but other branches are correct, the macro should work, if the invocation uses only correct branches</p>



<a name="159804793"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/159804793" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#159804793">(Mar 02 2019 at 16:33)</a>:</h4>
<p>So, instead of returning an <code>Result&lt;T, E&gt;</code>, I can imagine returning <code>(T, Vec&lt;E&gt;)</code>, where <code>T</code> is a best-effort parse, which ignores erroneous cases</p>



<a name="159804795"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/159804795" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#159804795">(Mar 02 2019 at 16:33)</a>:</h4>
<p>but for the start <code>Result&lt;T, E&gt;</code> should be fine as well</p>



<a name="159804844"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/159804844" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#159804844">(Mar 02 2019 at 16:34)</a>:</h4>
<p>Was just checking what rustc does here, it can give errors like "no rules expected the token <code>bar</code>"</p>



<a name="159804921"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/159804921" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#159804921">(Mar 02 2019 at 16:36)</a>:</h4>
<p>But the functionality could be different for IDE's, so maybe checking what rustc does isn't so helpful after all</p>



<a name="159805181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/159805181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#159805181">(Mar 02 2019 at 16:42)</a>:</h4>
<p>Yeah, best-effort parsing actually makes a lot more sense, would be nice to do it that way without first refactoring to Results</p>



<a name="159805341"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/159805341" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#159805341">(Mar 02 2019 at 16:46)</a>:</h4>
<p>on the one hand, yes, on the other hand, I think refactoring that should not be too difficult</p>



<a name="159805365"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/159805365" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#159805365">(Mar 02 2019 at 16:47)</a>:</h4>
<p>Let's tart with changing <code>MacroRules::parse</code> to return an <code>Result&lt;MacroRules, MacroRulesError&gt;</code>?</p>



<a name="159811614"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/159811614" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#159811614">(Mar 02 2019 at 19:38)</a>:</h4>
<p>I've changed most options to results in <a href="https://github.com/rust-analyzer/rust-analyzer/issues/916" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/issues/916">rust-analyzer#916</a>, though I'm not sure about the error information yet. I was also toying with the idea of storing a <code>Result&lt;SmolStr&gt;</code> in <code>Var.kind</code>, but dropped that idea because it was getting complicated</p>



<a name="162399164"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/162399164" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edwin Cheng <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#162399164">(Apr 03 2019 at 05:08)</a>:</h4>
<p>Hi, im trying to porting mbe test from intetllij-rust. And i just found that currently our macro expansion code do not preserve white space. As i know rust-analyzer parser is lossless. But im not sure about macro expansion.</p>



<a name="162399807"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/162399807" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#162399807">(Apr 03 2019 at 05:26)</a>:</h4>
<p>It would be cool to preserve white space across macro expansion, but not super usedul</p>



<a name="162841572"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/162841572" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cole Lawrence <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#162841572">(Apr 08 2019 at 18:13)</a>:</h4>
<p>If you wanted to see the expansion of a macro, would we eventually then care about preserving whitespace? I'm thinking about first time macro authors where the macro may produce completely invalid code...</p>



<a name="162851954"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/162851954" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#162851954">(Apr 08 2019 at 20:22)</a>:</h4>
<p>eventually maaaybe....</p>



<a name="162851959"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer%23720%20macro_rules/near/162851959" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rust-analyzer.23720.20macro_rules.html#162851959">(Apr 08 2019 at 20:22)</a>:</h4>
<p>though I'd gave for just refomatting the expanded code</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>